#include"Stack.h"

void StackInit(ST* ps)
{
	assert(ps);
	ps->data = NULL;
	ps->size = ps->capacity = 0;
}
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("StackPush->realloc failed");
			exit(-1);
		}
		ps->capacity = newcapacity;
		ps->data = tmp;
	}
	ps->data[ps->size] = x;
	ps->size++;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->size > 0);
	ps->size--;
}
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->size > 0);
	return ps->data[ps->size - 1];
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->size;
}
void StackDestory(ST* ps)
{
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->capacity = ps->size = 0;
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->size == 0;
}


